﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using TaxBusiness.DAL;
using TaxBusiness.Model;

namespace TaxBusiness.BLL
{
    public class BusinessBLL
    {
        //public List<Model.DXYW> GetDXYW(int pageSize, int pageIndex, string whereStr, string keyWord, string order, out int count)
        //{
        //    BusinessDB businessdb = new BusinessDB();
        //    List<Model.DXYW> dxywList = new List<DXYW>();
        //    count = businessdb.GetCount(whereStr);
        //    dxywList = businessdb.GetDXYW(pageSize, pageIndex, whereStr, order, keyWord);
        //    return dxywList;
        //}

        public List<Model.Business> GetList(int pageSize, int pageIndex, string whereStr, string keyWord, string order, out int count)
        {
            BusinessDB businessdb = new BusinessDB();
            List<Model.Business> businessList = new List<Business>();
            count = businessdb.GetCount(whereStr);
            businessList = businessdb.GetAll(pageSize, pageIndex, whereStr, order, keyWord);
            return businessList;
        }

        public ChargingLog GetChargingLog(int businessid)
        {
            BusinessDB businessdb = new BusinessDB();
            List<ChargingData> chargingdata = new List<ChargingData>();
            ChargingLog charginglog = new ChargingLog();
            chargingdata = businessdb.GetChargingData(businessid);
            charginglog.total = chargingdata.Count;
            charginglog.rows = chargingdata;
            return charginglog;

        }

        public Business Get(int id)
        {
            BusinessDB businessdb = new BusinessDB();
            return businessdb.Get(id);
        }

        public int Save(Model.Business info)
        {
            BusinessDB businessdb = new BusinessDB();
            int i = 0;
            if (info.BusinessType2 != "" && info.BusinessType1 != "-1")
            {
                info.BusinessType = info.BusinessType2;
                //info.TypeName = info.TypeName2;
            }
            else if (info.BusinessType1 != "-1")
            {
                info.BusinessType = info.BusinessType1;
                //info.TypeName = info.TypeName1;
            }
            else
            {
                throw new Exception("未选择项目名称。");
            }
            try
            {
                i = businessdb.Save(info);
            }
            catch (Exception e)
            {
                throw new Exception("系统错误，请联系系统管理员:" + e.Message);
            }
            return i;
        }

        public int Update(Model.Business info)
        {
            BusinessDB businessdb = new BusinessDB();
            int i = 0;
            try
            {
                i = businessdb.Update(info);
            }
            catch (Exception e)
            {
                throw new Exception("系统错误，请联系系统管理员:" + e.Message);
            }
            return i;
        }

        public int UpdateOverTime(string[] ids, string result)
        {
            BusinessDB businessdb = new BusinessDB();
            int[] idArray = Array.ConvertAll(ids, s => int.Parse(s));
            int i = 0;
            try
            {
                i = businessdb.UpdateOverTime(idArray, result);
            }
            catch (Exception e)
            {
                throw new Exception("系统错误，请联系系统管理员:" + e.Message);
            }
            return i;
        }

        public int Delte(string[] ids)
        {
            BusinessDB businessdb = new BusinessDB();
            int[] idArray = Array.ConvertAll(ids, s => int.Parse(s));
            int i = 0;
            try
            {
                i = businessdb.Delete(idArray);
            }
            catch(Exception e)
            {
                throw new Exception("系统错误，请联系系统管理员:" + e.Message);
            }
            return i;
        }

        public int SaveCheck(Model.Charging charging)
        {
            BusinessDB businessdb = new BusinessDB();
            int i = 0;
            try
            {
                i = businessdb.UpdateCheck(charging);
            }
            catch (Exception e)
            {
                throw new Exception("系统错误，请联系系统管理员:" + e.Message);
            }
            return i;
        }

        public int SaveCharging(Model.ChargingData charging)
        {
            BusinessDB businessdb = new BusinessDB();
            int i = 0;
            try
            {
                i = businessdb.UpdateCharging(charging);
            }
            catch (Exception e)
            {
                throw new Exception("系统错误，请联系系统管理员:" + e.Message);
            }
            return i;
        }


        public int SaveBill(Model.Charging charging)
        {
            BusinessDB businessdb = new BusinessDB();
            int i = 0;
            try
            {
                i = businessdb.UpdateBill(charging);
            }
            catch (Exception e)
            {
                throw new Exception("系统错误，请联系系统管理员:" + e.Message);
            }
            return i;
        }

        public int UpdatePaidup(int id)
        {
            BusinessDB businessdb = new BusinessDB();
            int i = 0;
            try
            {
                i = businessdb.UpdatePaidup(id);
            }
            catch (Exception e)
            {
                throw new Exception("系统错误，请联系系统管理员:" + e.Message);
            }
            return i;
        }

        public List<PaymentType> GetPayment()
        {
            BusinessDB businessdb = new BusinessDB();

            return businessdb.GetPayment();

        }

        public List<CountBill> CountBill(string date1, string date2)
        {
            BusinessDB businessdb = new BusinessDB();

            return businessdb.CountBill(date1, date2);

        }

        public List<CountBillbyOffice> CountBillByOffice(string date1, string date2)
        {
            BusinessDB businessdb = new BusinessDB();

            return businessdb.CountBillByOffice(date1, date2);

        }

        public List<CountResult> CountBusiness(string countType, string date1, string date2)
        {
            BusinessDB businessdb = new BusinessDB();

            return businessdb.CountBusiness(countType, date1, date2);
        }

        public DataTable ListToDataTable(List<Business> businessList)
        {
            DataTable dt = new DataTable();
            Dictionary<string, string> cellName = new Dictionary<string, string>();
            cellName.Add("BusinessCode", "编号");
            cellName.Add("TheName", "客户名称");
            cellName.Add("Phone", "电话");
            cellName.Add("Contact", "联系人");
            cellName.Add("TypeName", "项目名称");
            cellName.Add("Charges", "合同金额");
            cellName.Add("BeginDate", "合同开始日期");
            cellName.Add("EndDate", "合同终止日期");
            cellName.Add("Introducer", "客户介绍人");
            cellName.Add("Agent", "项目经办人");
            cellName.Add("OfficeName", "所属部室");
            cellName.Add("DepartmentName", "所属分局");
            cellName.Add("OwingMoney", "欠费");
            cellName.Add("BillDate", "开票日期");
            cellName.Add("BillUnitName", "开票单位");
            cellName.Add("ChargingDate", "交款日期");
            cellName.Add("FillIn", "填表人");
            cellName.Add("Verifier", "审核");
            cellName.Add("PaymentType", "类型");
            cellName.Add("Paidup", "免除余款");
            foreach (string value in cellName.Values)
            {
                dt.Columns.Add(value);
            }
            foreach (var business in businessList)
            {
                DataRow dr = dt.NewRow();
                foreach (KeyValuePair<string, string> dic in cellName)
                {
                    Type type = business.GetType();
                    System.Reflection.PropertyInfo propertyinfo = type.GetProperty(dic.Key);
                    if (propertyinfo.GetValue(business, null) != null)
                    {
                        dr[dic.Value] = propertyinfo.GetValue(business, null).ToString();
                    }
                    else
                    {
                        dr[dic.Value] = "";
                    }
                }
                dt.Rows.Add(dr);
            }

            return dt;
        }
    }
}
